<%@ page import="java.sql.*" %>
<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2024/10/27
  Time: 17:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>系统增删改之后的返回页面</title>
  </head>
  <body>
  <%
    request.setCharacterEncoding("utf-8");

    // 表单提交的参数
    String number = request.getParameter("number");
    String name = request.getParameter("name");
    String birthday = request.getParameter("birthday");
    String className = request.getParameter("className");

  %>
  <%
    String user = "root";
    String password = "123456";
    String url="jdbc:mysql://localhost:3306/studb?serverTimezone=Asia/Shanghai";

    Class.forName("com.mysql.cj.jdbc.Driver");

    Connection conn = DriverManager.getConnection(url, user, password);

    Statement statement = conn.createStatement();

    String sql = "insert into student(number,name,birthday,stuclass)values('"+number+"','"+name+"','"+birthday+"','"+className+"')";
    try {
      statement.executeUpdate(sql); // update  delete  insert  都要使用executeUpdate
    } catch (SQLException e) {
      e.printStackTrace();
    }

    // 先插入后查询
    String sql01 = "select * from student";
    ResultSet rs = statement.executeQuery(sql01);
  %>
  <table border="1">
    <th colspan="4" style="font-size: x-large;alignment: center">学生信息表</th>
    <tr>
      <td>学号</td>
      <td>姓名</td>
      <td>出生日期</td>
      <td>所在班级</td>
    </tr>
      <% while(rs.next()){ %>
    <tr>
      <td><%=rs.getString(1)%></td>
      <td><%=rs.getString(2)%></td>
      <td><%=rs.getString("birthday")%></td>
      <td><%=rs.getString("stuclass")%></td>
    </tr>
      <%}%>
      <%
       // 先开后关 的原则
        rs.close();
        statement.close();
        conn.close();
    %>
  </body>
</html>
